<?php
namespace manage;

require_once __DIR__ . '/Lib/Base.php';

use \Model\UserModel;

use Workerman\Protocols\Http;
if(!Http::sessionStarted()) Http::sessionStart();
checkLogin();

// 权限检查
$funcs = getFuncs($_SESSION['admin_group']);
if(!in_array(28, $funcs)) {
	echo '<script type="text/javascript">location.href="dashboard.php";</script>';
	#Http::header('Location:dashboard.php');
	return;
}

global $plat_db;

$error = $success = NULL;
$now = time();
$today = date('Y-m-d');

$action = $_GET['action'] ?? '';
if($action == 'send') {
	$type = $_GET['type'] ?? '';
	$message = $_GET['message'] ?? '';
	$message = urldecode(trim($message));
	if(!in_array($type, ['notice','stopserver'])) {
		echo '<script type="text/javascript">alert("无效的消息类型");location.href="message.php";</script>';
		return;
	}
	if(!$message) {
		echo '<script type="text/javascript">alert("消息内容不可为空");location.href="message.php";</script>';
		return;
	}
	if($type == 'notice') {
		$msgid = 9902;
		$funcid = 29;
	} else if($type == 'stopserver') {
		$msgid = 9901;
		$funcid = 30;
	}
	if(!in_array($funcid, $funcs)) {
		echo '<script type="text/javascript">alert("没有权限发送这种类型的消息");location.href="message.php";</script>';
		return;
	}
	operate_log('message', 'send_message', ['消息类型'=>$type, '消息内容'=>$message]);
	\Channel\Client::publish('manage_push', ['send_message', $msgid, $message]);
}

?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>AdminLTE 2 | General Form Elements</title>
	<!-- Tell the browser to be responsive to screen width -->
	<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <link rel="stylesheet" href="http://gold-static.91changwan.com/web/manage_static/css/bootstrap4.min.css">
	<!-- Font Awesome -->
	<link rel="stylesheet" href="http://gold-static.91changwan.com/web/manage_static/bower_components/font-awesome/css/font-awesome.min.css">
	<!-- Ionicons -->
	<link rel="stylesheet" href="http://gold-static.91changwan.com/web/manage_static/bower_components/Ionicons/css/ionicons.min.css">
	<!-- daterange picker -->
	<link rel="stylesheet" href="http://gold-static.91changwan.com/web/manage_static/bower_components/bootstrap-daterangepicker/daterangepicker.css">
	<!-- Theme style -->
	<link rel="stylesheet" href="http://gold-static.91changwan.com/web/manage_static/dist/css/AdminLTE.min.css">
	<!-- AdminLTE Skins. Choose a skin from the css/skins
		 folder instead of downloading all of them to reduce the load. -->
	<link rel="stylesheet" href="http://gold-static.91changwan.com/web/manage_static/dist/css/skins/_all-skins.min.css">

	<link rel="stylesheet" href="http://gold-static.91changwan.com/web/manage_static/plugins/iCheck/all.css">
	<style>
		.small-box-footer {height:40px; line-height:35px;}
		.data_table th {font-size:14px; color:#666; font-weight:normal; width:180px; text-align:center;}
		.data_table td {font-size:18px; font-weight:normal;text-align:right;}
		.data_table td small {font-size:12px; color:#888;margin-left:3px;}
		.data_table td div {padding:0;}
		td,th {vertical-align:middle !important;}
		.filter_box {font-size:12px;}
		.filter_box div {border:1px solid #4b97c4; border-radius:5px; padding:4px 7px; margin-right:5px; margin-left:10px; display:inline-block; padding-right:0 !important;}
		.filter_box div strong {color:#155;}
		.filter_box div span.badge {position:relative; left:11px; top:-15px; cursor:pointer;}
		.filter_box div span.badge:hover {background:#f70 !important; color:#ff0 !important;}
		#control_table th {font-weight:normal;font-size:12px;background:#fafafa;color:#343;}
	</style>
</head>

<body class="hold-transition skin-blue sidebar-mini" style="background:#ecf0f5;">

<div style="position:absolute;right:16px;top:-2px;font-size:12px;color:#aaa;text-align:right;width:100%;">Bootstrap V4.0 Page</div>

<section class="content" id="vbody">

	<?php if($error) : ?>
		<div class="alert alert-warning alert-dismissible">
			<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
			<span><?php echo $error; ?></span>
		</div>
	<?php endif ?>
	<?php if($success) : ?>
		<div class="alert alert-success alert-dismissible">
			<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
			<h4><i class="icon fa fa-check"></i> 成功</h4>
			<?php echo $success; ?>
		</div>
	<?php endif ?>

	<div class="box box-primary">
		<div class="box-header with-border">
			<h3 class="box-title" style="font-weight:bold;"><i class="fa fa-search"></i> 消息推送</h3>
			<?php
			echo '<span style="font-size:12px;color:red;font-weight:bold;padding-left:30px;">发送后,消息实时推送到所有用户,请谨慎检查消息内容和类型!!!</span>';
			?>
		</div>

		<div class="box-body">
			<form action="message.php?action=send" method="get">
			<table class="table table-bordered table-striped dataTable table-sm">
				<tr>
					<th width="150">类型</th>
					<td>
						<label>
							<input required type="radio" name="type" class="minimal" value="notice" checked />
							普通公告
						</label>
						&nbsp;&nbsp;&nbsp;&nbsp;
						<label>
							<input required type="radio" name="type" class="minimal" value="stopserver" />
							<span style="color:red;font-weight:bold;">停服通知</span>&nbsp;&nbsp;<span style="font-weight:normal;color:#999;font-size:10px;">收到停服通知后,用户会被强制退出账号并跳转到登录界面</span>
						</label>
					</td>
				</tr>
				<tr>
					<th>消息内容</th>
					<td>
                        <textarea required class="form-control" rows="3" name="message" required></textarea>
                        <div style="font-size:12px;margin-top:10px;">
                            消息模板:
                            <div class="alert alert-dark" role="alert" style="font-size:12px;">&lt;div fontsize=20 fontcolor=#005292 outline=1,#ffffff&gt;尊敬的玩家您好, 我们将在5分钟后停服维护更新, 预计时间1小时, 感谢您的配合和支持！&lt;/div&gt;</div>
                        </div>
                    </td>
				</tr>
				<tr>
					<th>&nbsp;<input type="hidden" name="action" value="send" /></th>
					<td><button type="submit" class="btn btn-primary">确定发送</button></td>
				</tr>
			</table>
			</form>
		</div>
	</div>



	<div class="box box-primary">
		<div class="box-header with-border">
			<h3 class="box-title" style="font-weight:bold;"><i class="fa fa-search"></i> 最近推送消息</h3>
		</div>

		<div class="box-body">
			<table id="example1" class="table table-bordered table-striped dataTable table-hover table-sm" role="grid" aria-describedby="example1_info">
				<thead>
				<tr>
					<th class="" rowspan="1" colspan="1" width="60" style="text-align:center">ID</th>
					<th class="" rowspan="1" colspan="1" width="100" style="text-align:center;">消息类型</th>
					<th class="" rowspan="1" colspan="1" style="text-align:center;">消息内容</th>
					<th class="" rowspan="1" colspan="1" width="140" style="text-align:center">推送时间</th>
					<th class="" rowspan="1" colspan="1" width="120" style="text-align:center">推送人</th>
                    <th class="" rowspan="1" colspan="1" width="120" style="text-align:center">操作IP</th>
				</tr>
				</thead>
				<tbody>
				<?php

                global $manage_db;
                $total = $manage_db->select("count(id) as total")->from('operation_log')->where("module='message' and action = 'send_message'")->row();
                $total = $total['total'];
                $pagesize = 10;
                $total_page = ceil($total / $pagesize);
                $page = $_GET['page'] ?? 1;
                if($page < 1) $page = 1;
                if($page > $total_page) $page = $total_page;
                $start = ($page - 1) * $pagesize;
                $messages = $manage_db->select('*')->from('operation_log')->where("module='message' and action = 'send_message'")->orderByDesc(['id'])->offset($start)->limit($pagesize)->query();
                if(!empty($messages)) { foreach($messages as $m) {
                    $u = $manage_db->select('username')->from('user')->where("id=".$m['user'])->row();
                    $mdata = json_decode($m['params'], true);
                    ?>
					<tr role="row" class="odd">
						<td style="vertical-align:middle;text-align:center;"><?php echo $m['id']; ?></td>
                        <td style="vertical-align:middle;text-align:center;font-size:14px;"><?php echo $mdata['消息类型']=='stopserver' ? '<span style="color:#f00">停服通知</span>' : '普通公告'; ?></td>
                        <td style="vertical-align:middle;text-align:left;font-size:12px;"><textarea readonly style="width:100%;border:0;background:none;outline:none;"><?php echo $mdata['消息内容']; ?></textarea></td>
                        <td style="vertical-align:middle;text-align:center;font-size:12px;"><?php echo date('Y-m-d H:i:s', $m['time']); ?></td>
                        <td style="vertical-align:middle;text-align:center;font-size:12px;"><?php echo $u['username']; ?></td>
                        <td style="vertical-align:middle;text-align:center;font-size:12px;">
                            <a href="javascript:;" style="cursor:pointer;" data-toggle="modal" data-target="#modal-findip" ip="<?php echo long2ip($m['ip']); ?>">
                                <?php echo long2ip($m['ip']); ?>
                            </a>
                        </td>
					</tr>
				<?php } } ?>
				</tbody>
			</table>
			<?php pagination4('message.php', $_GET, $page, $total, $pagesize); ?>
		</div>
	</div>

    <!-- 查询IP -->
    <div class="modal" tabindex="-1" role="dialog" id="modal-findip">
        <div class="modal-dialog" role="document" style="max-width:300px;">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">IP归属地</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <div id="iamip" style="font-size:14px;"></div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>

</section>

<!-- jQuery 3 -->
<script src="http://gold-static.91changwan.com/web/manage_static/js/jquery.min.js"></script>
<script src="http://gold-static.91changwan.com/web/manage_static/js/jquery.cookie.js"></script>
<script src="http://gold-static.91changwan.com/web/manage_static/js/vue.min.js"></script>
<!-- Bootstrap 3.3.7 -->
<script src="http://gold-static.91changwan.com/web/manage_static/js/bootstrap4.min.js"></script>
<!-- Select2 -->
<script src="http://gold-static.91changwan.com/web/manage_static/bower_components/select2/dist/js/select2.full.min.js"></script>
<!-- InputMask -->
<script src="http://gold-static.91changwan.com/web/manage_static/plugins/input-mask/jquery.inputmask.js"></script>
<script src="http://gold-static.91changwan.com/web/manage_static/plugins/input-mask/jquery.inputmask.date.extensions.js"></script>
<script src="http://gold-static.91changwan.com/web/manage_static/plugins/input-mask/jquery.inputmask.extensions.js"></script>
<!-- date-range-picker -->
<script src="http://gold-static.91changwan.com/web/manage_static/bower_components/moment/min/moment.min.js"></script>
<script src="http://gold-static.91changwan.com/web/manage_static/bower_components/bootstrap-daterangepicker/daterangepicker.js"></script>
<!-- bootstrap datepicker -->
<script src="http://gold-static.91changwan.com/web/manage_static/bower_components/bootstrap-datepicker/dist/js/bootstrap-datepicker.min.js"></script>
<!-- bootstrap time picker -->
<script src="http://gold-static.91changwan.com/web/manage_static/plugins/timepicker/bootstrap-timepicker.min.js"></script>
<!-- SlimScroll -->
<script src="http://gold-static.91changwan.com/web/manage_static/bower_components/jquery-slimscroll/jquery.slimscroll.min.js"></script>
<!-- iCheck 1.0.1 -->
<script src="http://gold-static.91changwan.com/web/manage_static/plugins/iCheck/icheck.min.js"></script>
<!-- FastClick -->
<script src="http://gold-static.91changwan.com/web/manage_static/bower_components/fastclick/lib/fastclick.js"></script>
<!-- AdminLTE App -->
<script src="http://gold-static.91changwan.com/web/manage_static/dist/js/adminlte.min.js"></script>
<script type="text/javascript">
    var app = new Vue({
        el: '#vbody',
        data: {
        },
        watch: {
        },
        methods: {
        }
    });

    $(function() {

        $('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
            checkboxClass: 'icheckbox_minimal-blue',
            radioClass   : 'iradio_minimal-blue'
        })
        //Red color scheme for iCheck
        $('input[type="checkbox"].minimal-red, input[type="radio"].minimal-red').iCheck({
            checkboxClass: 'icheckbox_minimal-red',
            radioClass   : 'iradio_minimal-red'
        })
        $('input[type="checkbox"].flat-red, input[type="radio"].flat-red').iCheck({
            checkboxClass: 'icheckbox_flat-green',
            radioClass   : 'iradio_flat-green'
        });

        $('#modal-findip').on('show.bs.modal', function (e) {
            var modal = $(this);
            var btn = $(e.relatedTarget);
            var ip = btn.attr('ip');
            var res = '';
            $('#iamip').html('');
            $.ajax({
                url: 'player_action.php?action=getip&ip=' + ip,
                beforeSend: function() {
                },
                success: function(data) {
                    res = JSON.parse(data);
                    // $('#iamip').html('<strong>IP地址</strong>:<br />&nbsp;&nbsp;&nbsp;&nbsp;' + ip + '<br /><strong>归属信息</strong>: <br />'
                    //     + '&nbsp;&nbsp;&nbsp;&nbsp;国家:' + res.data.data.country + '<br />'
                    //     + '&nbsp;&nbsp;&nbsp;&nbsp;省份:' + res.data.data.region + '<br />'
                    //     + '&nbsp;&nbsp;&nbsp;&nbsp;城市:' + res.data.data.city + '<br />'
                    //     + '&nbsp;&nbsp;&nbsp;&nbsp;运营商:' + res.data.data.isp
                    // );
                    eval(res.data);
                    $('#iamip').html('<strong>IP地址</strong>:<br />&nbsp;&nbsp;&nbsp;&nbsp;' + ip + '<br /><strong>归属信息</strong>: <br />'
                        //+ '&nbsp;&nbsp;&nbsp;&nbsp;国家:' + res.data.data.country + '<br />'
                        + '&nbsp;&nbsp;&nbsp;&nbsp;省份:' + lo + '<br />'
                        + '&nbsp;&nbsp;&nbsp;&nbsp;城市:' + lc + '<br />'
                        //+ '&nbsp;&nbsp;&nbsp;&nbsp;运营商:' + res.data.data.isp
                    );
                }
            });
        });

        setTimeout(function() {
            $('.alert-success').fadeOut();
            if($('.alert-warning').find('span').html() && $('.alert-warning').find('span').html().length < 60) {
                $('.alert-warning').fadeOut();
            }
        }, 2000);
    });
</script>

</body>
</html>
